package lab_1;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Date;

/**
 *
 * @author grp02 - Pascal Charreix and Meike Paetz
 *
 * The SmartToursClient is the client for the Holiday Package supplier 
 * SmartTours where Holiday Packages will be provided.
 */
public class SmartToursClient {

    private SmartToursManager Manager;
    final String host = "rmi://192.168.6.127:1099/";

    /**
     *
     * Main creating 5 packages with some randoms values
     */
    public static void main(String argv[]) {
        int i;
        int[] tabId;
        String[] flightList;
        String[] hotelList;
        String[] countryList;
        long timestamp;
        long start;

        i = 0;
        timestamp = System.currentTimeMillis();
        tabId = new int[5];
        flightList = new String[5];
        flightList[0] = "Air France";
        flightList[1] = "Lufthansa";
        flightList[2] = "Fly Nordic";
        flightList[3] = "Golden Air";
        flightList[4] = "Viking Airlines";
        hotelList = new String[5];
        hotelList[0] = "Accor";
        hotelList[1] = "Mercure";
        hotelList[2] = "Ibis";
        hotelList[3] = "Hilton";
        hotelList[4] = "Formule 1";
        countryList = new String[5];
        countryList[0] = "Tyskland";
        countryList[1] = "Frankrike";
        countryList[2] = "Sverige";
        countryList[3] = "Amerika";
        countryList[4] = "Danmark";
        SmartToursClient client = new SmartToursClient();
        while (i < 5) {
            start = timestamp + (long) Math.random() * 10000L;
            tabId[i] = client.createHolidayPackages(countryList[(int) (Math.random() * 5)], new Date(start), new Date(start), hotelList[(int) (Math.random() * 5)], (int) (Math.random() * 5), flightList[(int) (Math.random() * 5)], (float) (1000.0f + Math.random() * 500));
            i++;
        }
        client.deleteHolidayPackages((int) (Math.random() * 5));
    }

    /*
     * SmartTours client constructor
     */
    public SmartToursClient() {
        try {
            Manager = (SmartToursManager) Naming.lookup(host + "grp02Manager");
        } catch (RemoteException re) {
            System.err.println("Remote exception while looking up manager: " + re.getMessage());
        } catch (NotBoundException nbe) {
            System.err.println("Server object not found at expected URL: " + nbe.getMessage());
        } catch (MalformedURLException mue) {
            System.err.println("Bad RMI URL given for server object: " + mue.getMessage());
        }
    }

    /**
     *
     * creates Holiday Package
     */
    private int createHolidayPackages(String Country, Date Start, Date End, String HotelName, int HotelStars, String FlightOperator, float Price) {
        int id = 0;
        try {
            System.out.print("Creating Holiday Package...\t");
            id = Manager.createHolidayPackage(Country, Start, End, HotelName, HotelStars, FlightOperator, Price);
            System.out.println("Done.");
        } catch (RemoteException re) {
            System.err.println("Remote exception while creating Packages : " + re.getMessage());
        }
        return id;
    }

    /**
     *
     * deletes Holiday Package
     */
    private void deleteHolidayPackages(int id) {
        try {
            System.out.print("Deleting Holiday Package...\t");
            Manager.deleteHolidayPackage(id);
            System.out.println("Done.");
        } catch (RemoteException re) {
            System.err.println("Remote exception while deleting Packages : " + re.getMessage());
        } catch (NotBoundException nbe) {
            System.err.println("Server object not found at expected URL: " + nbe.getMessage());
        }
    }
}
